package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.sgb.kjwl.model.entity.user.MaterChooseEntity;
import com.sgb.kjwl.model.entity.user.TwoOrThreeLevelEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.fragment.material.AllCommodityFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2021/4/5 0005
 * 说明:
 */
public class AllCommodityModule extends BaseViewModel {
    private AllCommodityFragment mFragment;
    private MaterialRepertory mRepertory;

    public AllCommodityModule(AllCommodityFragment fragment, MaterialRepertory repertory) {
        this.mFragment = fragment;
        this.mRepertory = repertory;
    }

    public void getTwoOrThreeLevelData(String id, int type) {
        NewAPIManager.getInstance().getGoodsAPI().findTwoOrThreeLevelData(mRepertory.getAreaCode(), id).enqueue(new Callback<BaseEntity<List<TwoOrThreeLevelEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Response<BaseEntity<List<TwoOrThreeLevelEntity>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (type == 0) {
                        mFragment.setTwoLevelData(response.body().getData());
                    } else {
                        mFragment.setThreeLevelData(response.body().getData());
                    }
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Throwable t) {
            }
        });
    }


    public void getMaterialBrand(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("areaCode", mRepertory.getAreaCode());
        map.put("id", id);
        APIManager.getInstance().getGoodsAPI().getMaterChoose(map).enqueue(new Callback<BaseEntity<List<MaterChooseEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MaterChooseEntity>>> call, Response<BaseEntity<List<MaterChooseEntity>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<MaterChooseEntity> list = response.body().getData();
                    for (MaterChooseEntity materChooseEntity : list) {
                        materChooseEntity.getType().add(0, new MaterChooseEntity.TypeBean("全部", ""));
                    }
                    mFragment.setMaterialBrandData(list);
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MaterChooseEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getGoodsList(Map<String, Object> map) {
        map.put("areaCode", mRepertory.getAreaCode());
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getGoodsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<GoodsListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, Response<BaseEntity<ListResponse<List<GoodsListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getGoodsList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}